package com.example.noobpackage.ui.selfpackage

import android.graphics.Canvas
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import com.example.noobpackage.tool.ItemTouchHelperAdapter
import kotlin.math.abs

class SelfPackageItemTouchHelperCallback(private val myAdapter: ItemTouchHelperAdapter) :
    ItemTouchHelper.Callback() {

    override fun getMovementFlags(
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder
    ): Int {
        val dragFlag = 0
        val swipeFlag = ItemTouchHelper.RIGHT
        return makeMovementFlags(dragFlag, swipeFlag)
    }

    override fun onMove(
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder,
        target: RecyclerView.ViewHolder
    ): Boolean {
        return true
    }

    override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
        myAdapter.onItemDismiss(viewHolder.adapterPosition)
    }

    override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
        super.clearView(recyclerView, viewHolder)
        val foreGround = (viewHolder.itemView as ViewGroup).getChildAt(1)
        val backGround = (viewHolder.itemView as ViewGroup).getChildAt(0)
        val text = ((backGround as ViewGroup).getChildAt(0) as ViewGroup).getChildAt(1) as TextView
        val icon = (backGround.getChildAt(0) as ViewGroup).getChildAt(0) as ImageView
        // 重置变化
        foreGround.translationX = 0F
        backGround.alpha = 0.2F
        text.text = "Done"
        icon.visibility = View.INVISIBLE
    }

    override fun onChildDraw(
        c: Canvas,
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder,
        dX: Float,
        dY: Float,
        actionState: Int,
        isCurrentlyActive: Boolean
    ) {
        val foreGround = (viewHolder.itemView as ViewGroup).getChildAt(1)
        val backGround = (viewHolder.itemView as ViewGroup).getChildAt(0)
        val text = ((backGround as ViewGroup).getChildAt(0) as ViewGroup).getChildAt(1) as TextView
        val icon = (backGround.getChildAt(0) as ViewGroup).getChildAt(0) as ImageView

        if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE && !myAdapter.getIsPublished(
                viewHolder.adapterPosition
            )
        ) {
            backGround.alpha = 0.2F
            if (abs(dX) <= recyclerView.width * 3 / 5) {
                val rate = 1 - abs(recyclerView.width * 3 / 5 - dX) / (recyclerView.width * 3 / 5)
                foreGround.translationX = dX
                backGround.alpha = (rate * 0.8 + 0.2).toFloat()
                if (abs(dX) >= recyclerView.width / 2) {
                    text.text = ""
                    icon.visibility = View.VISIBLE
                }
            } else if (abs(dX) <= recyclerView.width * 4 / 5) {
                val rate = abs(recyclerView.width * 4 / 5 - dX) / (recyclerView.width * 1 / 5)
                foreGround.translationX = dX
                backGround.alpha = rate
            } else {
                foreGround.translationX = 0F
                super.onChildDraw(
                    c,
                    recyclerView,
                    viewHolder,
                    dX,
                    dY,
                    actionState,
                    isCurrentlyActive
                )
            }
        } else {
            return
        }
    }
}